var markerArray = new Array();
var newMarkerId;
var currentMarker = null;
var googleMap;
var mapBounds;
var infoWindow;

function showInfo () {
    infoWindow.setContent(this.html);
    infoWindow.open(googleMap, this);
}

function addMarker(areaName, areaInfo, lat, lng, dragEnabled) {
    var thisMarkerId = '[' + newMarkerId++ + ']';
    var markerLocation = new google.maps.LatLng(lat, lng);
    mapBounds.extend(markerLocation);
    currentMarker = new google.maps.Marker({ title: areaName, position: markerLocation, map: googleMap, draggable: dragEnabled, html: areaInfo });
    currentMarker.markerId = thisMarkerId;
    markerArray[thisMarkerId] = currentMarker;
    // Eventos del pushpin
    google.maps.event.addListener(currentMarker, "rightclick", showInfo);
    google.maps.event.addListener(currentMarker, "dblclick", showInfo);
    return thisMarkerId;
}

function createAffectedAreasMap(mapId) {    
    var markerAmount = document.getElementById(mapId + "_marker_amount").value;    
    var markerLocation = new google.maps.LatLng(-34.91799473178151, -56.166508197784424);
    
	var options = { zoom: 5, center: markerLocation, mapTypeId: google.maps.MapTypeId.TERRAIN };
	googleMap = new google.maps.Map(document.getElementById(mapId), options);
    mapBounds = new google.maps.LatLngBounds();
    infoWindow = new google.maps.InfoWindow({ content: "Loading..." });

    for (newMarkerId = 0; newMarkerId < markerAmount; ) {
        var thisMarkerId = '[' + newMarkerId + ']';
	    addMarker(document.getElementsByName(thisMarkerId + '.AreaName')[0].value,
                document.getElementsByName(thisMarkerId + '.AreaInfo')[0].value,
                document.getElementsByName(thisMarkerId + '.Latitude')[0].value,
                document.getElementsByName(thisMarkerId + '.Longitude')[0].value, false);
	}
	if (currentMarker == null) {
	    googleMap.setZoom(1);
	    googleMap.setCenter(markerLocation);
	} else if (markerAmount == 1) {
	    googleMap.setCenter(currentMarker.getPosition());
	} else {    
	    googleMap.fitBounds(mapBounds);
	}
}